package com.example.innospace.demos.web.service.Impl;

import com.example.innospace.demos.pojo.dto.AdminDTO;
import com.example.innospace.demos.pojo.dto.AdminPageQueryDTO;
import com.example.innospace.demos.pojo.entity.Admin;
import com.example.innospace.demos.pojo.result.PageResult;
import com.example.innospace.demos.web.mapper.AdminMapper;
import com.example.innospace.demos.web.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin findById(String adminId) {
        return adminMapper.findById(adminId);
    }

    @Override
    public String updateAminInfo(Admin admin) {
        adminMapper.updateAminInfo(admin);
        return null;
    }

    @Override
    public void insert(AdminDTO adminDTO) {
        adminDTO.setPassword("ksd@"+adminDTO.getAdminId());
        adminDTO.setAvatar("https://spacea.oss-cn-beijing.aliyuncs.com/14a40fac-4185-4d94-824f-efe83fcb60f0-195.jpg");
        adminDTO.setStatus(1);
        adminMapper.insert(adminDTO);
    }

    @Override
    public boolean batchDeleteById(List<String> adminIds) {
        for(int i=0;i<adminIds.size();i++)
        {
            Admin admin=adminMapper.findById(adminIds.get(i));
            if(admin.getStatus()==1)
            return false;
        }
        // TODO 在status为0时才可以删除账号
        adminMapper.batchDeleteById( adminIds);
        return true;
    }

    @Override
    public void adminstartOrstop(Integer status, String adminId) {
        adminMapper.adminstartOrstop(status,adminId);
    }

    public PageResult pageQuery(int pageNum, int pageSize, AdminPageQueryDTO adminPageQueryDTO) {

        PageHelper.startPage(pageNum,pageSize);
        Page<Admin> page1=adminMapper.pageQuery(adminPageQueryDTO);
        long total=page1.getTotal();
        List<Admin> records=page1.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void updateAvatar(String adminId, String fileUrl) {
        adminMapper.updateAvatar(adminId,fileUrl);
    }

    @Override
    public String getAvatar(String adminId) {
        return adminMapper.getAvatar(adminId);
    }


}
